<template>
	<view>
		<!-- 自定义头部导航 -->
		<view class="page">
			<view class="upload-scan">
				<scan-bar id="scanInput" ref="scanBarRef" placeholder="扫描工序流转单" @code-scanned="getScanResult"
					:key="datekey"></scan-bar>
			</view>
			<view class="area-header">
				<u-text :text="'已扫描的工序流转单('+unitList.length+')'">
				</u-text>
			</view>
			<view class="list-content">
				<!-- 列表 -->
				<u-list class="list" v-if="unitList && unitList.length > 0" :height="ListViewHeight">
					<u-list-item v-for="(item, index) in unitList" :key="item.id">
						<view class="card">
							<view class="right">
								<view class="title">
									工序流转单: {{item.produceTransferFormCode}}
								</view>
								<view class="body">
									工作中心:{{ item.workCenterCode }}-{{ item.workCenterName }}
								</view>
								<view class="body">
									<label class="span">批次号:{{ item?.remarks || '待定'}}</label>
								</view>
								<view class="body">
									<label class="span">物料:{{ item.materialCode }}-{{ item.materialName }}</label>
								</view>
								<view class="body">
									<label class="span">数量: {{item?.productQuantity || '待定'}}</label>
								</view>
							</view>
							<view class="left">
								<u-icon name="close-circle-fill" color="#f56c6c" size="30px"
									@click="removeDetail(item,index)"></u-icon>
							</view>
						</view>
						<u-line></u-line>
					</u-list-item>
				</u-list>
				<!-- 空数据 -->
				<u-list v-else :height="ListViewHeight">
					<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
				</u-list>
			</view>
			<u-line></u-line>
			<view class="search">
				<u-button type="primary" @click="confirmReceiveData" class="custom-style"
					:loading="posting">提交</u-button>
			</view>
		</view>
		<u-modal :show="messageShow" :closeOnClickOverlay="false" title="提示" content="是否删除此工序流转单信息"
			:showCancelButton="true" @confirm="messageConfirm" @cancel="messageCancel"></u-modal>
	</view>
</template>

<script setup lang="ts">
	import { computed, ref } from 'vue';
	import ScanBar from '@/components/scan-bar/index.vue';
	import { onShow, onLoad } from '@dcloudio/uni-app';
	import { http } from "../../../utils/http";
	import { useWmsStore } from '@/stores/useWmsStore';
	import { PageResult } from '../../../types/type';

	// 重置表单信息 & 光标自动blur
	const scanBarRef = ref();
	const datekey = ref(new Date());
	const scanCnt = ref(0);
	const posting = ref(false);
	const ListViewHeight = ref(0);
	const messageShow = ref(false);

	const receiveDataBack = {
		mesExteriorDeliveryUnitDTOS: [],
	};


	onShow(async () => {
		ListViewHeight.value = uni.getSystemInfoSync().windowHeight - 160;
		console.log('ListViewHeight.value', ListViewHeight.value);
	})

	//分页
	const current = ref(0)
	const unitList = ref([])
	// 获取扫码结果
	function getScanResult(result) {
		console.log('扫码结果', result);
		let hasBarCode = true;
		unitList.value.forEach((item) => {
			if (item.externalCode == result) {
				uni.$u.toast('重复扫描！');
				hasBarCode = false
				scanBarRef.value.reset()
			}
		})

		if (hasBarCode) {
			queryByPage(result)
		}
		scanBarRef.value.reset();
		datekey.value= new Date();
	}
	async function queryByPage(result) {
		if (!result) {
			return
		}
		const res : PageResult<any> = await http({
			url: '/mes/mesexteriordelivery/getProductList',
			method: 'POST',
			data: {
				current: current.value,
				size: 20,
				data: {
					externalCode: result
				}
			}
		}).finally(
			scanBarRef.value.reset()
		);
		if (res.code == 200) {
			if (res.data.length == 1) {
				unitList.value.splice(0, 0, res.data[0]);
			} else {
				uni.$u.toast('未找到工序流转单！');
			}
		}
	}

	async function confirmReceiveData() {
		if (unitList.value.length > 0) {
			const res = await http({
				url: '/mes/mesexteriordelivery/save',
				method: 'POST',
				data: {
					mesProduceTransferFormDTOS: unitList.value
				}
			});
			if (res.code == "200") {
				uni.$u.toast('外协发货创建成功！');
				uni.navigateBack({
					delta: 1, //返回层数，2则上上页
				})
			}
		} else {
			uni.$u.toast('工序流转单不能为空！');
		}
	}
	const deleteIndex = ref(0);
	const deliveryUnit = ref()
	function removeDetail(item : any, index : any) {
		deleteIndex.value = index;
		messageShow.value = true;
		deliveryUnit.value = item;
	}
	async function messageConfirm() {
		messageShow.value = false;
		unitList.value.splice(deleteIndex.value, 1);
		uni.$u.toast('删除成功！');
	}

	function messageCancel() {
		messageShow.value = false;
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
	}

	.upload-scan {
		padding: 10px 40rpx 10px 40rpx;
	}

	.area-header {
		color: #909193;
		padding: 10px 15px;
		margin-bottom: 0;
		font-size: 15px;
		background-color: #f3f4f6;
		display: flex;
	}

	.list-content {
		padding: $uni-page-padding;

		.list {
			.card {
				margin-top: 5px;
				margin-bottom: 10px;
				border-radius: 7px;
				display: flex;
				align-items: center;
				padding: 10px 0;
				transition: all 300ms ease;
				box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

				.left {
					// flex-basis: 90rpx;
					padding-right: 10px;
					text-align: right;
					font-size: 13px;

					.enter {
						line-height: 20px;
						margin-bottom: 5px;
					}

					.number {
						font-size: 13px;
						line-height: 18px;
					}

					:deep(.u-icon--right) {
						flex-direction: row-reverse;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;

					.title {
						font-size: 18px;
						line-height: 20px;
						margin-bottom: 5px;
						font-weight: 700;
					}

					.body {
						font-size: 14px;
						line-height: 18px;
					}
				}
			}
		}
	}

	.search {
		display: flex;
		flex: 1;

		.custom-style {
			// width: 120px;
			margin: 12px 5px 5px;
			height: 40px;
			border-radius: 8px;

			&:nth-child(1) {
				flex: 1
			}

			&:nth-child(2) {
				flex: 3
			}
		}
	}
</style>